// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class AdManage extends cc.Component {

    public static instance: AdManage = null;

    onLoad() {
        if (!AdManage.instance) {
            AdManage.instance = this;
        }
        this.init();
    }
    /**
     * 平台
     */
    platform: string = "";
    platformApi: any = null;
    systemInfo: any = null;
    viodeAd: any = null;
    bannerAd: any = null;


    init() {
        if (this.platform == "wx") {
            this.platformApi = window["wx"];
            this.systemInfo = this.platformApi.getSystemInfoSync();
        } else if (this.platform == "oppo" || this.platform == "vivo") {

        }

    }

    /**
     * 激励视频广告
     * @param id 
     * @param callback 
     */
    initRewardedVideoAd(id: string = "", callback: Function = null) {
        if (this.platform == "wx") {
            let version = this.compareVersion(this.systemInfo.SDKVersion, "2.6.0");
            if (version) {
                this.platformApi.showLoading({
                    title: "请稍等..."
                })
                if (!this.viodeAd) {
                    this.viodeAd = this.platformApi.createRewardedVideoAd({
                        adUnitId: id
                    })
                }
                this.viodeAd.load()
                    .then(() => {
                        console.log("激励视频加载成功");
                    })
                    .catch((err: any) => {
                        this.platformApi.hideLoading();
                        console.log(err, "激励视频加载失败");
                    })
                this.viodeAd.onLoad(() => {
                    this.platformApi.hideLoading();
                    console.log("激励视频拉取成功");
                    this.viodeAd.show()
                        .then().catch((err: any) => {
                            console.log(err, "激励视频显示失败");
                            this.viodeAd.load()
                                .then(() => {
                                    console.log("激励视频加载成功");
                                })
                                .catch((err: any) => {
                                    this.platformApi.showToast({
                                        title: "视频显示失败"
                                    })
                                    console.log(err, "激励视频加载失败");
                                })
                        })
                })
                this.viodeAd.onError((err: any) => {
                    this.platformApi.hideLoading();
                    console.log(err, "激励视频拉取失败");
                })
                this.viodeAd.onClose((res: any) => {
                    if (res && res.isEnded) {
                        callback && callback(1);
                    } else {
                        this.platformApi.showToast({
                            title: "中途关闭"
                        })
                        callback && callback(0);
                    }
                })
            }
        } else if (this.platform == "oppo") {

        } else if (this.platform == "vivo") {

        }
    }
    /**
     * banner广告
     * @param id 
     */
    initBannerAd(id: string = "") {
        if (this.platform == "wx") {
            let version = this.compareVersion(this.systemInfo.SDKVersion, "2.0.4");
            if (version) {
                this.bannerAd = this.platformApi.createBannerAd({
                    adUnitId: id
                })

                this.bannerAd.onResize((res: any) => {
                    this.bannerAd.width = res.width;
                    this.bannerAd.height = res.height;
                    this.bannerAd.top = this.systemInfo.screenHeight - res.height;
                    this.bannerAd.left = (this.systemInfo.screenWidth - res.width) / 2;
                })
                

            }
        } else if (this.platform == "oppo") {

        } else if (this.platform == "vivo") {

        }
    }
    /**
     * 插屏广告
     * @param id 
     */
    initInterstitialAd(id: string = "") {
        if (this.platform == "wx") {

        } else if (this.platform == "oppo") {

        } else if (this.platform == "vivo") {

        }
    }
    /**
     * 原生广告
     * @param id 
     */
    initCustomAd(id: string = "") {
        if (this.platform == "wx") {

        } else if (this.platform == "oppo") {

        } else if (this.platform == "vivo") {

        }
    }

    /**
     * 判断版本号是否达标
     * @param curV 当前的版本
     * @param reqV 最低版本
     */
    compareVersion(curV: string, reqV: string) {
        let lVersion = curV.split("."),
            sVersion = reqV.split('.'),
            lVersionNum = parseInt(lVersion[0]) * 1000000 + parseInt(lVersion[1]) * 10000 + parseInt(lVersion[2]) * 100,
            sVersionNum = parseInt(sVersion[0]) * 1000000 + parseInt(sVersion[1]) * 10000 + parseInt(sVersion[2]) * 100;
        if (lVersionNum < sVersionNum) {
            return false
        }
        return true
    }

}
